import {app, BrowserWindow, screen} from 'electron'

var package_ = require("../../package.json");
const path = require('path');
var electron = require('electron');
import '../renderer/store'

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
    global.__static = path.join(__dirname, '/static').replace(/\\/g, '\\\\')
}

process.env['PYTHON_PATH'] = '/usr/local/bin/python';

let mainWindow;
const winURL = process.env.NODE_ENV === 'development'
    ? `http://localhost:9080`
    : `file://${__dirname}/index.html`;

function createWindow() {
    let preload = path.join(__dirname, 'preload.js');
    console.log("preload : ", preload);


    let icon = path.join(__dirname, 'static/icon.ico');

    var isDev = process.env.NODE_ENV === 'development';
    // isDev = true;

    if (isDev) icon = path.join(__dirname, '../../static/icon.icns');

    let title = package_.title + " (V" + package_.version + ")";
    // let w = screen.getPrimaryDisplay().workAreaSize.width - 23;
    // if (w > 1920) {
    //     w = 1920;
    // }

    let w = 1920;
    let h = 1080;
    let console_w = 0;
    if (isDev) {
        console_w = 320;
    }
    w = w + console_w;

    console.log("=================================");
    console.log(title);
    console.log(w, h);
    console.log("=================================");

    /**
     * Initial window options
     */
    mainWindow = new BrowserWindow({
        title: title,
        //自定义窗口
        // frame:false,
        width: w,
        height: h,
        icon: icon,//Extra
        useContentSize: true,
        // 窗口在屏幕居中
        center: true,
        // 窗口是否可以改变尺寸 开启后maximizable失效
        resizable: isDev,
        webPreferences: {
            webSecurity: false,//解决本地图片无法加载问题
            // 是否开启 DevTools,开发模式默认位true
            devTools: isDev,
            // preload: preload,
            contextIsolation: false,
            nodeIntegration: true,
            nodeIntegrationInWorker: true,
            webviewTag: true,
            enableRemoteModule: true, // 使用remote模块
        }
    });

    mainWindow.loadURL(winURL);
    mainWindow.setMenu(null);

    //TODO 开启调试模式
    if (isDev) {
        mainWindow.webContents.openDevTools();
    }

    mainWindow.on('close', (e) => {
        e.preventDefault();

        electron.dialog.showMessageBox({
            type: 'info',
            title: '提示',
            message: '确定要退出吗？',
            buttons: ['确认', '取消'],   //选择按钮，点击确认则下面的idx为0，取消为1
            cancelId: 1, //这个的值是如果直接把提示框×掉返回的值，这里设置成和“取消”按钮一样的值，下面的idx也会是1
        }).then(idx => {
            //注意上面↑是用的then，网上好多是直接把方法做为showMessageBox的第二个参数，我的测试下不成功
            if (idx.response == 1) {
                console.log('index==1，取消关闭');
                e.preventDefault();
            } else {
                console.log('index==0，关闭');
                mainWindow = null;
                app.exit();
            }
        })
    })
}

app.on('ready', createWindow);

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit()
    }
});

app.on('activate', () => {
    if (mainWindow === null) {
        createWindow()
    }
});

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
